package com.wondersgroup.fjzlpt.console.service.impl.user;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.quick.framework.boot.remoteResult.ResultObject;
import com.wondersgroup.fjzlpt.console.dao.inter.role.RoleDAO;
import com.wondersgroup.fjzlpt.console.dao.inter.role.RoleMenuDAO;
import com.wondersgroup.fjzlpt.console.dao.inter.user.UserDAO;
import com.wondersgroup.fjzlpt.console.dao.inter.user.UserRoleDAO;
import com.wondersgroup.fjzlpt.console.service.inter.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * Created by lion on 2017/5/26.
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDAO userDAO;
    @Autowired
    private UserRoleDAO userRoleDAO;
    @Autowired
    private RoleDAO roleDAO;
    @Autowired
    private RoleMenuDAO roleMenuDAO;

    /**
     * 分页查询用户信息
     */
    public JSONObject queryPage(Map<String, String> map){
        return this.userDAO.queryPage(map);
    }

    /**
     * 删除用户
     */
    public JSONObject delete(Map<String, String> map){
        ResultObject ro = new ResultObject();
        try{
            this.userDAO.deleteForPrimaryKey(map);
            String userId = map.get("USERID");
            this.userRoleDAO.deleteByUserId(userId);
        }catch(Exception e){
            ro.changeFaultState("删除用户失败！");
            throw new RuntimeException("删除用户失败！", e);
        }
        return (JSONObject) JSON.toJSON(ro);
    }

    /**
     * 启用/禁用用户
     */
    public JSONObject updateUserState(Map<String, String> map){
        ResultObject ro = new ResultObject();
        try{
            this.userDAO.updateForPrimaryKey(map);
        }catch(Exception e){
            ro.changeFaultState("更新用户状态失败！");
            throw new RuntimeException("更新用户状态失败！", e);
        }

        return (JSONObject) JSON.toJSON(ro);
    }

    /**
     * 保存用户信息
     * @param map
     * @return
     */
    public JSONObject save(Map<String, String> map){
        ResultObject ro = new ResultObject();
        String method = map.remove("method");
        if("insert".equals(method)){
            insert(map, ro);
        }else if("update".equals(method)){
            update(map, ro);
        }
        return (JSONObject) JSON.toJSON(ro);
    }

    private void insert(Map<String, String> map, ResultObject ro){
        try {
            String username = map.get("USERNAME");
            String userId = map.get("USERID");
            if(userDAO.isExistUsername(username, userId)){
                ro.changeFaultState("该用户名已存在！");
            }else{
                map.put("STATE", "1");
                userDAO.insert(map);
                String roleId = map.get("ROLEID");
                this.userRoleDAO.insert(userId, roleId);
            }

        } catch (Exception e) {
            ro.changeFaultState("新增用户失败！");
            throw new RuntimeException("新增用户失败！", e);
        }
    }

    private void update(Map<String, String> map, ResultObject ro){
        try {

            if(userDAO.isExistUsername(map.get("USERNAME"),map.get("USERID"))){
               ro.changeFaultState("该用户名已存在！");
            }else{
                userDAO.updateForPrimaryKey(map);
                String userId = map.get("USERID");
                String roleId = map.get("ROLEID");
                this.userRoleDAO.deleteByUserId(userId);
                this.userRoleDAO.insert(userId, roleId);
            }
        } catch (Exception e) {
            ro.changeFaultState("修改用户信息失败！");
            throw new RuntimeException("修改用户信息失败！", e);
        }
    }

    @Override
    public JSONObject queryManagerInitData(String roleType){
        JSONObject json = new JSONObject();
        //String jgid = ptjgid;
        /*角色信息*/
        JSONArray roleAry = queryRoleAry(roleType);
        json.put("roleAry", roleAry);

        return json;
    }

    private JSONArray queryRoleAry(String roleScope){
        //String roleScope = "";
//        if(!SystemConstant.PTJGID.equals(ptjgid)){
//            roleScope = RoleScope.ORG.getValue();
//        }
        return roleDAO.querySummary(roleScope);
    }

    /**
     * 查询角色菜单
     * @param roleId
     * @return
     */
    public JSONArray queryRoleMenu(String roleId){
        return roleMenuDAO.queryMenusByRoleId(roleId);
    }
}
